package myAsm;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.persistence.*;


import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;


@Entity
@DiscriminatorValue("INSN_LOOKUPSWITCH")
public class MyLookupSwitchInsnNode extends MyAbstractInsnNode {

	  /**
     * Beginning of the default handler block.
     */
	@Transient
    public MyLabelNode dflt;

    /**
     * The values of the keys. This list is a list of {@link Integer} objects.
     */
	@Transient
    public List keys;

    /**
     * Beginnings of the handler blocks. This list is a list of
     * {@link LabelNode} objects.
     */
	@Transient
    public List labels;

    public MyLookupSwitchInsnNode(){
            super(-1);
        }
	
    /**
     * Constructs a new {@link LookupSwitchInsnNode}.
     * 
     * @param dflt beginning of the default handler block.
     * @param keys the values of the keys.
     * @param labels beginnings of the handler blocks. <tt>labels[i]</tt> is
     *        the beginning of the handler block for the <tt>keys[i]</tt> key.
     */
    public MyLookupSwitchInsnNode(
        final MyLabelNode dflt,
        final int[] keys,
        final MyLabelNode[] labels)
    {
        super(Opcodes.LOOKUPSWITCH);
        this.dflt = dflt;
        this.keys = new ArrayList(keys == null ? 0 : keys.length);
        this.labels = new ArrayList(labels == null ? 0 : labels.length);
        if (keys != null) {
            for (int i = 0; i < keys.length; ++i) {
                this.keys.add(new Integer(keys[i]));
            }
        }
        if (labels != null) {
            this.labels.addAll(Arrays.asList(labels));
        }
    }

    public int getType() {
        return LOOKUPSWITCH_INSN;
    }

    public void accept(final MethodVisitor mv) {
        int[] keys = new int[this.keys.size()];
        for (int i = 0; i < keys.length; ++i) {
            keys[i] = ((Integer) this.keys.get(i)).intValue();
        }
        Label[] labels = new Label[this.labels.size()];
        for (int i = 0; i < labels.length; ++i) {
            labels[i] = ((MyLabelNode) this.labels.get(i)).getLabel();
        }
        mv.visitLookupSwitchInsn(dflt.getLabel(), keys, labels);
    }

    public MyAbstractInsnNode clone(final Map labels) {
        MyLookupSwitchInsnNode clone = new MyLookupSwitchInsnNode(clone(dflt,
                labels), null, clone(this.labels, labels));
        clone.keys.addAll(keys);
        return clone;
    }
}
